//
//  ImageSlideAndButtonsViewController.m
//  ubixar-framework
//
//  Created by Demis Bellot on 23/04/2009.
//  Copyright 2009 Ubixar. All rights reserved.
//

#import "ImageSlideAndButtonsViewController.h"
#import "UXUI.h";
#import "UXSystem.h";


@implementation ImageSlideAndButtonsViewController
@synthesize imageView;
@synthesize images;
@synthesize buttons;
@synthesize selectedIndex;

- (id)initWithCoder:(NSCoder *)coder {		
	if (self = [super initWithCoder:coder]) {
		DLog(@"initializing");
		selectedIndex = 0;
	}
	return self;
}

- (void)setImageNames:(NSArray*)imageNames 
		  buttonNames:(NSArray*)buttonNames
	 firstButtonFrame:(CGRect)firstButtonFrame
		buttonPadding:(CGPoint)buttonPadding
{
	DLog(@"v1.1 imageNames %@, buttonNames: %@", imageNames, buttonNames);

	NSMutableArray* aArray = [[[NSMutableArray alloc] init] retain];
	images = aArray;
	[aArray release];
	
	aArray = [[[NSMutableArray alloc] init] retain];
	buttons = aArray;
	[aArray release];
	
	CGRect nextButtonFrame;
	NSUInteger imageNamesCount = [imageNames count];
	for (int i=0; i<imageNamesCount; i++) 
	{
		SLog(@"working no %d of %d", i+1, imageNamesCount);
		NSString* imageName = [imageNames objectAtIndex:i];
		[images addObject:[UIImage imageNamed:imageName]];
		
		if (i==0)
		{
			DLog(@"loading imageView");
			UIImageView* aImageView = [[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)] retain];
			imageView = aImageView;
			[aImageView release];
			imageView.image = (UIImage*)[images objectAtIndex:selectedIndex];
			imageView.contentMode = UIViewContentModeTopLeft;
			[self.view addSubview:imageView];

			nextButtonFrame = firstButtonFrame;
		}
		else
		{
			if (buttonPadding.x != 0)
			{
				nextButtonFrame.origin.x += buttonPadding.x > 0
				? nextButtonFrame.size.width + buttonPadding.x
				: -nextButtonFrame.size.width - buttonPadding.x;
			}

			if (buttonPadding.y != 0)
			{
				nextButtonFrame.origin.y += buttonPadding.y > 0
				? nextButtonFrame.size.height + buttonPadding.y
				: -nextButtonFrame.size.height - buttonPadding.y;
			}
		}
		
		UIButton* imageButton = [[UIButton alloc] initWithFrame:nextButtonFrame];
		
		NSString* buttonName = [buttonNames objectAtIndex:i];
		[imageButton setImagesUsingImageNameFormat:buttonName];

		[imageButton addClickHandler:@selector(imageButtonWasPressed:) forTarget:self];
		imageButton.selected = (i == selectedIndex);
		if (imageButton.selected)
		{
			lastSelectedButton = [imageButton retain];
		}
		
		imageButton.tag = i;
		[buttons addObject:imageButton];

		[self.view addSubview:imageButton];
		[imageButton release];
	}
}

- (void)imageButtonWasPressed: (UIButton*)button 
{
	//if the same button is pressed, just toggle the button.
	//the selectedIndex remains unchanged
	if (lastSelectedButton && lastSelectedButton == button)
	{
		lastSelectedButton.selected = !lastSelectedButton.selected;
		return;
	}
	
	//Otherwise de-select the lastButton if exists
	if (lastSelectedButton) 
	{
		lastSelectedButton.selected = FALSE;
	}
	
	//And set the current state
	[self setSelectedIndex:(NSUInteger)button.tag];
	lastSelectedButton = button;
	lastSelectedButton.selected = TRUE;
}

- (void)setSelectedIndex: (NSUInteger)aSelectedIndex {
	DLog(@"changing selectedIndex from %d to %d");
	if (selectedIndex != aSelectedIndex)
	{
		selectedIndex = aSelectedIndex;
		imageView.image = [self currentImage];
	}
}

- (NSUInteger) selectedIndex {
	return selectedIndex;
}

- (UIImage*) currentImage {
	DLog(@"getting image at %d", selectedIndex);
	return (UIImage*) [images objectAtIndex:selectedIndex];
}

/*
// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        // Custom initialization
    }
    return self;
}
*/

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/

/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
}
*/

/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
    // Release anything that's not essential, such as cached data
}


- (void)dealloc {
	[imageView release];
	[images release];
	[buttons release];
    [super dealloc];
}


@end
